package com.ca.project.Server.Modules.TweetsImg.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ca.project.Server.Modules.TweetsImg.Context.ReturnPicContext;
import com.ca.project.Server.Modules.TweetsImg.Context.UploadImgContext;
import com.ca.project.Server.Modules.TweetsImg.Converter.TweetsImgConverter;
import com.ca.project.Server.Modules.TweetsImg.Entity.TweetsImg;
import com.ca.project.Server.Modules.TweetsImg.PO.ReturnPicPO;
import com.ca.project.Server.Modules.TweetsImg.PO.UploadImgPO;
import com.ca.project.Server.Modules.TweetsImg.Service.TweetsImgService;
import com.ca.project.Server.Modules.TweetsImg.VO.DeleteImgVO;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.TweetsImg.Controller
 * @NAME: TweetsImgController
 * @USER: ASUS
 * @DATE: 31/12/2024
 * @TIME: 下午3:46
 **/
@RestController
@RequestMapping("/tweets_img")
@Api(tags = "商品图片模块")
public class TweetsImgController {

    @Autowired
    private TweetsImgService tweetsImgService;
    @Autowired
    private TweetsImgConverter tweetsImgConverter;

    @ApiOperation(
            value = "图片上传接口",
            notes = "该接口提供了图片上传的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/upload")
    public R Upload(@Validated UploadImgPO uploadImgPO) {
        UploadImgContext uploadImgContext =
                tweetsImgConverter.UploadImgPO2UploadImgContext(uploadImgPO);
        TweetsImg entity = tweetsImgService.upload(uploadImgContext);
        return R.data(entity);
    }

    @ApiOperation(
            value = "图片删除接口",
            notes = "该接口提供了图片删除的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/delete")
    public R delete(@Validated @RequestBody DeleteImgVO deleteImgVO) {
        String filename = deleteImgVO.getT_img_key();
        tweetsImgService.delete(filename);
        return R.success();
    }

    @ApiOperation(
            value = "查询所有图片信息-分页",
            notes = "该接口提供了查询所有图片信息-分页的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/{currentPage}/{PageSize}")
    public R AllImg(@PathVariable Integer currentPage, @PathVariable Integer PageSize, TweetsImg tweetsImg) {
        IPage<TweetsImg> page = tweetsImgService.getPage(currentPage, PageSize, tweetsImg);
        if (currentPage > page.getPages())
            page = tweetsImgService.getPage((int) page.getPages(), PageSize);
        return R.data(page);
    }

    @ApiOperation(
            value = "根据推文名称分类返回推文图片接口",
            notes = "该接口提供了根据推文名称分类返回推文图片的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/picture")
    public R ReturnPicByCN(@Validated @RequestBody ReturnPicPO returnPicPO){
        ReturnPicContext returnPicContext =
                tweetsImgConverter.ReturnPicPO2ReturnPicContext(returnPicPO);
        return R.data(tweetsImgService.getPic(returnPicContext));
    }


}
